package com.css.apps.base.menu.action;

import java.util.List;

import com.css.apps.base.menu.model.SMenu;
import com.css.apps.base.sys.action.SysItem;
import com.css.apps.base.sys.model.SSys;
import com.css.core.model.tree.TreeCache;
import com.css.core.model.tree.TreeNode;
import com.css.db.query.QueryCache;
import com.css.util.StringHelper;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class MenuItem {

	public static JSONObject getRootMenu() throws JSONException {
		TreeNode root = new TreeCache(SMenu.class).getRoot().get(0);
		SMenu item = QueryCache.get(SMenu.class, root.getNodeId());
		if (item == null)
			return null;
		JSONObject one = new JSONObject();
		one.put("id", item.getMenuId());
		one.put("name", item.getMenuName());
		one.put("pId", item.getParentId());
		one.put("isParent", true);
		one.put("open", true);
		return one;
	}

	public static JSONArray getSubMenu1(String parentId) throws JSONException {

		TreeNode tn = new TreeCache(SMenu.class).getTreeNode(parentId);
		List<TreeNode> lst = tn.getChildren();
		JSONArray menuTree = new JSONArray();
		JSONObject one;
		for (TreeNode node : lst) {
			SMenu item = QueryCache.get(SMenu.class, node.getNodeId());
			if (item != null) {
				one = new JSONObject();
				one.put("id", node.getNodeId());
				one.put("name", item.getMenuName());
				one.put("pId", item.getParentId());
				one.put("isParent", !node.isLeaf());
				menuTree.add(one);
			}
		}
		return menuTree;
	}

	public static JSONArray getSubMenu(String parentId) throws JSONException {
		JSONArray jsonArray = new JSONArray();
		JSONObject one;
		if (StringHelper.isEmpty(parentId)) {
			// one = new JSONObject();
			// one.put("id", "0");
			// one.put("name", "系统菜单");
			// one.put("pId", "0");
			// one.put("sysId","1001");
			// one.put("isParent", true);
			// one.put("chkDisabled",true);
			// one.put("open", true);
			// jsonArray.add(one);
			List<SSys> sysList = SysItem.getSystems();
			if (sysList != null)
				for (SSys item : sysList) {
					one = new JSONObject();
					one.put("id", item.getUuid());
					one.put("name", item.getName());
					one.put("pId", "0");
					one.put("sysId", item.getUuid());
					one.put("isParent", true);
					one.put("chkDisabled", true);
					// one.put("open", true);
					jsonArray.add(one);
				}
		} else {
			List<SMenu> lst = getMenus(parentId);
			List<String> ids;
			if (lst != null)
				for (SMenu item : lst) {
					one = new JSONObject();
					one.put("id", item.getMenuId());
					one.put("name", item.getMenuName());
					one.put("sysId", item.getSysId());
					one.put("pId", item.getParentId());
					// one.put("url", item.getUrl());
					one.put("funcId", item.getFuncId());
					ids = getMenuIds(item.getMenuId());
					if (ids == null || ids.size() < 1)
						one.put("isParent", false);
					else
						one.put("isParent", true);
					jsonArray.add(one);
				}
		}

		return jsonArray;
	}

	@SuppressWarnings("unchecked")
	public static List<SMenu> getMenus(String parentId) {
		QueryCache qc = getMenuIdsQC(parentId);
		List<String> list = qc.list();
		return QueryCache.idToObj(SMenu.class, list);
	}

	private static QueryCache getMenuIdsQC(String parentId) {
		return new QueryCache("select a.menuId from SMenu a where a.parentId=:parentId order by a.orderNum")
				.setParameter("parentId", parentId);
	}

	@SuppressWarnings("unchecked")
	public static List<String> getMenuIds(String parentId) {
		QueryCache qc = getMenuIdsQC(parentId);
		return qc.listCache();
	}
}
